#include "Device/encoder/Encoder_RT1064.h"

void Encoder::Init()
{
    qtmr_config_t qtmrConfig;
	//tmr1 C0 C1
		qtmrConfig.primarySource = phase_a;
    qtmrConfig.secondarySource = phase_b;
		QTMR_Init(base, channel, &qtmrConfig);
    QTMR_Deinit(base, channel);           
    QTMR_Init(base, channel, &qtmrConfig);
    QTMR_StartTimer(base, channel, clockSource);

    QTMR_SetLoadValue(base,channel,32767);
		Reload();
    raw_data = last_raw_data = 32767;
}

uint16_t Encoder::getEncoder()
{
    return QTMR_GetCurrentTimerCount(base,channel);
}

void Encoder::Reload()
{
    base->CHANNEL[channel].CNTR = 32767;
}

void Encoder::UpdateRaw()
{
    last_raw_data = raw_data;
    raw_data = getEncoder();
	Reload();
}

int32_t Encoder::getRawEncoder()
{
    return raw_data;
}
